'use strict';

const Controller = require('egg').Controller;

class CollectController extends Controller {
  async index() {
    const { ctx } = this;
    var {id,collected} = ctx.request.body;
    collected = (collected === 'true')? true:false
    const db = this.app.mongodb;
    const res = await ctx.service.movies.getMovieDetailOrSearch({id:id, collected:collected});
    var item = res[0];
    try {
      if (collected) {
        await db.collection('user').insertOne(item);
        ctx.body = {
          status: 200,
          msg: '收藏成功',
        };
      } else {
        await db.collection('user').deleteOne({ _id:id });
        ctx.body = {
          status: 200,
          msg: '取消收藏',
        };
      }
    } catch (err) {
      this.app.logger.error(err);
    }
  }
}

module.exports = CollectController;
